home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / SAS-C / sc655pch / guiprof / timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  2.8 KB  |  117 lines

  1. /*-------------------------------------------------------------------*/
  2. /* Copyright (c) 1993 by SAS Institute Inc., Cary NC                 */
  3. /* All Rights Reserved                                               */
  4. /*                                                                   */
  5. /* SUPPORT:    walker - Doug Walker                                  */
  6. /*-------------------------------------------------------------------*/
  7.  
  8. #include <exec/ports.h>
  9. #include <devices/timer.h>
  10. #include <dos/dosextens.h>
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13. #include <string.h>
  14.  
  15. #include "sprofpriv.h"
  16.  
  17. struct TimerPacket 
  18. {
  19.    struct timerequest tm_req;
  20.    struct DosPacket   tm_pkt;
  21. };
  22.  
  23. static struct TimerPacket *timerpkt;
  24. static struct MsgPort *timerport;
  25. static int ioout, deviceopen;
  26.  
  27. void GetTimerPkt(long repost, int wait)
  28. {
  29.    if(timerpkt != NULL && ioout)
  30.    {
  31.       if(wait || CheckIO((struct IORequest *)&timerpkt->tm_req))
  32.       {
  33.          /* If the IO request is already complete, the AbortIO */
  34.          /* won't do anything, and the WaitIO will simply      */
  35.          /* dequeue the request and return.                    */
  36.          AbortIO((struct IORequest *)&timerpkt->tm_req);
  37.          WaitIO((struct IORequest *)&timerpkt->tm_req);
  38.          ioout = 0;
  39.       }
  40.    }
  41.    if(repost) PostTimerReq(repost);
  42. }
  43.  
  44. void _STDCloseTimer(void)
  45. {
  46.    if(ioout)
  47.    {
  48.       GetTimerPkt(0,1);
  49.       ioout = 0;
  50.    }
  51.    if(timerpkt)
  52.    {
  53.       if(deviceopen)
  54.       {
  55.          CloseDevice((struct IORequest *)&(timerpkt->tm_req));
  56.          deviceopen = 0;
  57.       }
  58.       DeleteExtIO((struct IORequest *)timerpkt);
  59.       timerpkt = NULL;
  60.    }
  61.    if(timerport)
  62.    {
  63.       DeletePort(timerport);
  64.       timerport = NULL;
  65.    }
  66. }
  67.  
  68. long OpenTimer(void)
  69. {
  70.    int error;
  71.  
  72.    if(timerport || timerpkt) return(0);
  73.  
  74.    if(!(timerport = CreatePort(NULL, NULL)))
  75.       return(0);
  76.  
  77.    if ((timerpkt = (struct TimerPacket *)
  78.       CreateExtIO(timerport, sizeof(struct TimerPacket)))== NULL)
  79.    {
  80.       return(0);
  81.    }
  82.  
  83.    timerpkt->tm_req.tr_node.io_Message.mn_Node.ln_Name = 
  84.       (char *)&(timerpkt->tm_pkt);
  85.    timerpkt->tm_pkt.dp_Link = &(timerpkt->tm_req.tr_node.io_Message);
  86.    timerpkt->tm_pkt.dp_Port = timerport;
  87.  
  88.    error = OpenDevice(TIMERNAME, UNIT_MICROHZ,
  89.                    (struct IORequest *)&(timerpkt->tm_req), 0);
  90.  
  91.    if(error)
  92.    {
  93.       _STDCloseTimer();
  94.       return(0);
  95.    }
  96.    deviceopen = 1;
  97.  
  98.    return(1<<timerport->mp_SigBit);
  99. }
  100.  
  101. void PostTimerReq(long time)  // time is in thousandths of a second
  102. {
  103.    if(time == 0) return;
  104.  
  105.    if (timerpkt != NULL && !ioout)
  106.    {
  107.       timerpkt->tm_req.tr_node.io_Command = TR_ADDREQUEST;
  108.       timerpkt->tm_req.tr_time.tv_secs = time/1000;
  109.       timerpkt->tm_req.tr_time.tv_micro = (time%1000)*1000;
  110.       timerpkt->tm_pkt.dp_Type = ACTION_TIMER;
  111.  
  112.       SendIO((struct IORequest *)&timerpkt->tm_req);
  113.       ioout = 1;
  114.    }
  115. }
  116.  
  117.